package com.industry.staff.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.industry.framework.core.generic.AbstractCoreGenericService;
import com.industry.staff.controller.vo.DepartmentVo;
import com.industry.staff.dal.dataobject.Department;
import com.industry.staff.dal.db.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author nbZhuozh
 * @Date 2025-04-10  16:01
 **/

@Service
public class DepartmentService extends AbstractCoreGenericService<Department, String> {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> batchInsert(List<Department> list) {
        getMapper().insert(list, 500);
        List<String> resultIds = new ArrayList<>();
        list.forEach(i->{
            Method method = ReflectionUtils.findMethod(getEntityClass(), "getId");
            if(method != null){
                String idValue =String.valueOf( ReflectionUtils.invokeMethod(method, i));
                resultIds.add(idValue);
            }
        });
        //判断当前业务类是否需要记录
        if(activeRecordBiz(getEntityClass())){
            doMarkRecordTypeInsert(resultIds);
        }
        return resultIds;
    }


    public List<DepartmentVo> buildTree(List<Department> psource){
        List<Department> departments = departmentMapper.selectList(Wrappers.lambdaQuery());
        return DepartmentVo.buildTree(departments);
    }

}